/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;
import exatrez.dao.RefShipVisitMatchDAO;
import exatrez.dao.ShipVisit3DAO;
import exatrez.model.InfoAudit;
import exatrez.model.Observation;
import exatrez.model.RefShipVisitMatch;
import exatrez.model.ShipVisit3;
import exatrez.model.ShipVisit3Sub;
import exatrez.model.User;
import exatrez.stripes.ShipVisitConfiguration;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mansur
 */
public class ShipVisit3IntegrationManager {
    
    private static Logger LOG = LoggerFactory.getLogger(ShipVisit3IntegrationManager.class);
    
    @Inject
    private ShipVisit3DAO shipVisit3DAO;
    @Inject
    private ShipVisit3Manager sv3Manager;
    @Inject
    ShipVisitConfiguration svconf;
    
    @Transactional
    public void ShipVisit3Observation(Observation obs, User user){
        LOG.debug("method ShipVisit3Observation start > user.id :"+user.getUserId());
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
        LOG.debug("obsId :"+obs.getObservationId());
        LOG.debug("obs.vessel :"+obs.getVessel().getVesselName());
        LOG.debug("obs.location :"+obs.getLocation());
        LOG.debug("user.department :"+user.getDepartment().getDepartmentId()+", Name :"+user.getDepartment().getDepartmentName());
        try{
            ShipVisit3 sv3 = new ShipVisit3();
            sv3.setDeleted("N");
            sv3.setStatus("K");
            sv3.setVessel(obs.getVessel());
            sv3.setLocation(obs.getLocation());
            sv3.setDateVisit(obs.getDateOfInspection());
            sv3.setUser(user);
            sv3.setWorkflow(user.getDepartment().getShipVisitProcessFlow());
            sv3.setDepartmentCurrent(user.getDepartment());
            InfoAudit ia = new InfoAudit();
            ia.setCreateId(user);
            ia.setCreateDate(new Date());
            sv3.setInfoAudit(ia);
            
            List<ShipVisit3Sub> list = new ArrayList<ShipVisit3Sub>();
            List<RefShipVisitMatch> matchs = sv3Manager.getListShipVisitMatch(user.getDepartment().getDepartmentId());
            int c = 0;
            for (RefShipVisitMatch m : matchs) {
                ShipVisit3Sub sub = new ShipVisit3Sub();
                sub.setShipVisit3(sv3);
                sub.setRefShipVisitHeader(m.getShipVisitHeader());
                sub.setInputType(m.getShipVisitHeader().getInputType());
                sub.setInfoAudit(ia);
                if(c == 0)
                    sub.setDetails(svconf.getProperty("form.details.ma"));
                else
                    sub.setDetails("Visit to "+obs.getVessel().getVesselName()+" on date "+sdf.format(obs.getDateOfInspection()));
                list.add(sub);
                c++;
            }
            sv3.setListShipVisitSub(list);
            
            shipVisit3DAO.save(sv3);
        }catch(Exception ex){
            LOG.error(ex.getMessage());
            ex.printStackTrace();//for development use only
        }
        LOG.debug("method ShipVisit3Observation finish > user.id :"+user.getUserId());
    }
}
